<template>
  <el-dialog v-model="props.cancelDialog" title="关闭点检" width="25%">
    <el-form :model="form" ref="formRef">
      <el-form-item
        label="关闭原因"
        prop="closeReason"
        :rules="{
          required: true,
          message: '请输入关闭原因',
          trigger: 'blur'
        }"
      >
        <el-input
          v-model="form.closeReason"
          autocomplete="off"
          type="textarea"
          maxlength="100"
          show-word-limit
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="resetForm(formRef)">取消</el-button>
        <el-button
          type="primary"
          @click="submitForm(formRef)"
          :loading="loading"
        >
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import { ElMessage, FormInstance } from 'element-plus'
import { ref, reactive } from 'vue'
import { cancelMaintainTaskApi } from '/@/api/check'
const props = defineProps({
  cancelDialog: Boolean,
  cancelId: String
})

const emit = defineEmits(['closeCancel', 'closeSubmit'])
const formRef = ref<FormInstance>()
const loading = ref(false)
const form = ref({
  closeReason: ''
})

const submitForm = async (formEl: FormInstance | undefined) => {
  loading.value = true
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      Reflect.set(form.value, 'id', props.cancelId)
      cancelMaintainTaskApi(form.value).then((res) => {
        if (res.code == 0) {
          ElMessage.success('已取消')
          loading.value = false
          emit('closeSubmit')
        } else {
          loading.value = false
        }
      })
    } else {
      loading.value = false
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  emit('closeCancel')
}
</script>
<style scoped lang="scss"></style>
